#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import json
from flask import request, Flask, jsonify
# from concurrent.futures import ThreadPoolExecutor
import os
import re
import time

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False


# executor = ThreadPoolExecutor(10)


def generate_venom_name():
    '''生成venom数据文件名称'''
    timestamp = time.time()
    timeArray = time.localtime(timestamp)
    time_str = time.strftime("%Y-%m-%d", timeArray)
    timestamp_str = str(timestamp).split('.')[0]
    log_name = './output/log/1362.log.{}-{}'.format(time_str, timestamp_str)
    return log_name

@app.route('/bzy', methods=['POST'])
def handle_file_data():
    file_contant = request.files.get('file')
    file_path = generate_venom_name()
    file_contant.save(file_path)

    if os.path.exists(file_path):
        res_info = {'msg': 'recv {}'.format('saved'), 'code': 1}
    else:
        res_info = {'msg': 'recv {}'.format('unsaved'), 'code': 0}
    return jsonify(res_info), 201

@app.route('/date', methods=['GET'])
def handle_date_log():
    date_str = request.args.get('date','').strip()
    log_file_path = './output/recv/{}.log'.format(date_str)
    if not os.path.exists(log_file_path):
        res_info = {'msg': 'no such date', 'code': 0}
        return jsonify(res_info), 201
    else:
        print('{} is exists'.format(log_file_path))
    result_dic = {}
    index = 0
    with open(log_file_path,'r') as f:
        for line in f:
            line = line.strip()
            if line:
                if index < 2:
                    print(line)
                ls = [i.strip() for i in re.split(r'\t| |(/home/spider)',line) if i and i.strip()]
                # ls = [i.strip() for i in line.split(' ') if i.strip()]
                if len(ls) >= 3:
                    if index < 2:
                        print(ls)
                    source = ls[0]
                    count = ls[1]
                    file_name = ls[2]
                    if count.isdigit():
                        result_dic[source] = result_dic.setdefault(source,0) + int(count)
                else:
                    print('长度:{}'.format(len(ls)))
                index += 1
    res_info = {'msg': result_dic, 'code': 1}
    return jsonify(res_info), 201


if __name__ == '__main__':
    # 9999端口，接收八爪鱼数据文件存到output/log目录，
    # 每隔3分钟，上传到oss，然后删除，再新建解析任务进行解析
    app.run(debug=False, host='0.0.0.0', port=9999)


# source ../qcc/venv/bin/activate
# nohup python bzy_api.py &
# nohup python result_log_uploader.py &
